/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package anivis.ui;


import anivis.base.Notification;
import anivis.base.RestrictionNotification;
import anivis.db.QueryLayer;
import anivis.db.QueryLayer.RestrictionType;
import anivis.layers.Layer;
import anivis.layers.ObservationLayer;
import java.sql.SQLException;
import java.util.GregorianCalendar;
import java.util.Observable;
import java.util.Set;
import java.util.logging.Level;
import java.util.logging.Logger;
import prefuse.data.Table;
/**
 *
 * @author urp
 */
public class Controller extends java.util.Observable implements java.util.Observer
{
  
  private QueryLayer    queryLayers;
  
  private ObservationLayer layers;
  
  public Controller()
  {
      try {
          queryLayers=new QueryLayer();
          queryLayers.addObserver(this);
          GregorianCalendar high = new GregorianCalendar();
          GregorianCalendar low = new GregorianCalendar(1900, GregorianCalendar.JANUARY, 1);
          setRestriction(new QueryLayer.RangeRestriction(low.getTimeInMillis() / 1000.0, high.getTimeInMillis() / 1000.0, RestrictionType.TimeWindow));
      }
      catch (java.sql.SQLException sqle) {
          System.err.println(sqle);
      }
      catch (ClassNotFoundException cnfe) {
          System.err.println(cnfe);
      }
  }
  
  public QueryLayer getQueryLayer() {
      return queryLayers;
  }

  public Table getTable(Set<RestrictionType> r) 
  { 
    Table table = null;
    try 
    {
      table = queryLayers.getData(r);
      System.out.println("CONTROLLER.getTable: " + table.getRowCount());
    } catch (SQLException ex) 
    {
      Logger.getLogger(Controller.class.getName()).log(Level.SEVERE, null, ex);
      System.err.println(ex);
    }
   
    return table;
  }
  
  public boolean setRestriction(QueryLayer.Restriction r)
  {
    System.out.println("CONTROLLER.setRestriction: " + r);
    boolean returnValue = queryLayers.setRestriction(r);
    setChanged();
    notifyObservers(new RestrictionNotification(r));
    return returnValue;
  }
    
  public boolean clearRestriction(QueryLayer.RestrictionType t)
  {
    return queryLayers.clearRestriction(t);
  }
  
  public void update(Observable o, Object arg) 
  {
     System.out.println("CONTROLLER.update: " + arg);
     setChanged();
     notifyObservers(arg);
  }     
  
  public void prepareData()
  {
    System.out.println("CONTROLLER.prepareData");
    try {
        queryLayers.prepareData();
    }
    catch (SQLException sqle) {
        System.err.println(sqle);
    }
    
  }
 
}
